package com.example.bookstore.mapper;

import com.example.bookstore.entity.Address;
import com.example.bookstore.entity.People;
import com.example.bookstore.entity.ShopCart;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface PeopleMapper {


    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @Select("select * from people where username = #{username} and password = #{password}")
    People Login(@Param("username") String username,@Param("password") String password);


    /**
     * 注册
     * @param username
     * @param password
     * @return
     */
    @Update("insert into people values (0,#{username},#{password},0)")
    Integer Register(@Param("username") String username,@Param("password") String password);


    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @Select("select * from people where username = #{username}")
    People findByUserName(String username);


    /**
     * 根据用户id和book id查询购物车找到1个或0个对应的商品
     * @param uid
     * @param id
     * @return
     */
    @Select("select * from shopcart where user_id = #{uid} and book_id =#{id}")
    ShopCart findByTwo(Integer uid,Integer id);


    /**
     * 将商品添加到购物车
     * @param shopCart
     * @return
     */
    @Insert("insert into shopcart values (0,#{bookId},#{userId},#{name},#{image},#{number},#{price},#{amount})")
    Integer add(ShopCart shopCart);


    /**
     * 根据用户id和商品id查询购物车，对该商品数量加一
     * @param uid
     * @param id
     * @return
     */
    @Update("update shopcart set number = number +1,amount = amount+price where user_id=#{uid} and book_id = #{id}")
    Integer updateByUidAndIid(@Param("uid") Integer uid,@Param("id") Integer id);


    /**
     * 根据用户id查询购物车
     * @param id
     * @return
     */
    @Select("select * from shopcart where user_id = #{id}")
    List<ShopCart> queryShopcart(Integer id);


    /**
     * 根据用户id查找全部购物车商品
     * @param id
     * @return
     */
    @Select("select * from shopcart where user_id = #{id}")
    List<ShopCart> findSbAllShopCart(Integer id);



    /**
     * 根据用户id和商品id查询购物车，对该商品数量减一
     * @param uid
     * @param iid
     * @return
     */
    @Update("update shopcart set number = number -1,amount = amount-price where user_id=#{uid} and book_id = #{iid}")
    Integer updateByUidAndIid2(@Param("uid") Integer uid,@Param("iid") Integer iid);



    /**
     * 删除购物车(根据id删除)
     * @param id
     * @return
     */
    @Delete("delete from shopcart where id =#{id}")
    Integer delete(Integer id);


    /**
     * 设置用户地址
     * @param userId
     * @param address
     * @param phone
     * @return
     */
    @Insert("insert into address values (0,#{userId},#{address},#{phone});")
    Integer setAddress(@Param("userId")Integer userId,@Param("address")String address,@Param("phone")String phone);


    /**
     * 根据用户id查找地址信息
     * @param id
     * @return
     */
    @Select("select * from address where user_id =#{id}")
    Address findAddressByUserId(Integer id);


    /**
     * 根据用户id修改用户地址
     * @return
     */
    @Update("update address set detail = #{address},phone = #{phone} where user_id = #{id}")
    Integer updateAddress(@Param("id") Integer id,@Param("address") String address,@Param("phone") String phone);


    /**
     * 根据bookid删除书
     * @param id
     * @return
     */
    @Delete("delete from book where id = #{id}")
    Integer deleteBook(Integer id);


    /**
     * 清空购物车
     * @param id
     */
    @Delete("delete from shopcart where user_id = #{id}")
    void cleanShopCart(Integer id);

}
